/*
{*******************************************************************}
{                                                                   }
{       KS-GANTT DotNet Library                                     }
{       Gantt-Charting Control for Windows Forms                    }
{                                                                   }
{       Copyright (c) 2009 - 2012 by Kroll-Software                 }
{       All Rights Reserved                                         }
{                                                                   }
{   This code is provided 'as is',                                  }
{   without warranties of any kind. Use at your own risk.           }                                                              
{                                                                   }
{   As a license holder of the KS-Gantt Control,                    }
{   you may freely redistribute executables based on                }
{   and created with these contents.                                }
{   You can modify the source code to meet your needs.              }
{                                                                   }
{   However, you must not publish, redistribute or transfer         }
{   this source code to 3rd parties under any conditions.           }
{                                                                   } 
{*******************************************************************}
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KS.Common;

namespace MiGuava.SqlServer
{
    public partial class SelectProjectDialog : Form
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeGanttDB()
        {
            return false;
        }

        private GanttSqlDB m_GanttDB = null;
        public GanttSqlDB GanttDB
        {
            get
            {
                return m_GanttDB;
            }
            set
            {
                m_GanttDB = value;
                if (m_GanttDB != null)
                {
                    FillProjects();
                }
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeProjectKey()
        {
            return false;
        }

        private string m_ProjectKey = "";
        public string ProjectKey
        {
            get
            {
                return m_ProjectKey;
            }
            set
            {
                m_ProjectKey = value;
            }
        }


        public SelectProjectDialog()
        {
            InitializeComponent();

            lsvProjects.Columns.Add(Properties.Resources.ColumnProject, 80);
            lsvProjects.Columns.Add(Properties.Resources.ColumnDescription, 160);            

            lsvProjects.Columns[0].Width = lsvProjects.Width / 2 - 16;
            lsvProjects.Columns[1].Width = lsvProjects.Width / 2 - 16;

            lsvProjects.FullRowSelect = true;
            
            lsvProjects.SelectedIndexChanged += new EventHandler(lsvProjects_SelectedIndexChanged);
            lsvProjects.DoubleClick += new EventHandler(lsvProjects_DoubleClick);

            lblInfo.Text = Properties.Resources.SQLSelectProjectInfo;
        }        

        private void FillProjects()
        {            
            lsvProjects.Items.Clear();

            System.Data.SqlClient.SqlDataReader dr = null;

            try
            {
                dr = m_GanttDB.ExecuteSQL("select ItemKey, ItemText, Description from Projects ORDER BY ItemText");

                ListViewItem liSelected = null;

                while (dr.Read())
                {
                    ListViewItem item = new ListViewItem(Strings.GetSafeString(dr["ItemText"]));
                    item.SubItems.Add(Strings.GetSafeString(dr["Description"]));                    
                    item.Tag = Strings.GetSafeString(dr["ItemKey"]);
                    lsvProjects.Items.Add(item);

                    if ((string)item.Tag == m_ProjectKey)
                    {
                        item.Selected = true;
                        liSelected = item;
                    }
                }

                dr.Close();                

                if (lsvProjects.Items.Count > 0)
                {
                    if (liSelected != null)
                        liSelected.EnsureVisible();
                    else
                        lsvProjects.Items[0].Selected = true;
                }
                else
                    cmdOK.Enabled = false;

                m_ProjectKey = "";
            }
            catch (Exception ex)
            {
                Program.ErrMsgBox(ex.Message);
                cmdOK.Enabled = false;                
                return;
            }
            finally
            {
                if (dr != null)
                    dr.Dispose();

                m_ProjectKey = "";
            }            
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            lsvProjects.Focus();            
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            if (lsvProjects.Columns.Count > 1)
            {
                lsvProjects.Columns[0].Width = lsvProjects.Width / 2 - 16;
                lsvProjects.Columns[1].Width = lsvProjects.Width / 2 - 16;
            }
        }
        
        void lsvProjects_SelectedIndexChanged(object sender, EventArgs e)
        {            
            cmdOK.Enabled = lsvProjects.SelectedItems.Count > 0;
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (lsvProjects.SelectedItems.Count > 0)
                m_ProjectKey = lsvProjects.SelectedItems[0].Tag.ToString();
            
            this.DialogResult = DialogResult.OK;

            this.Close();
        }                    

        void lsvProjects_DoubleClick(object sender, EventArgs e)
        {
            if (lsvProjects.SelectedItems.Count > 0)
                cmdOK_Click(null, null);
        }
                
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            m_ProjectKey = "";            
            this.DialogResult = DialogResult.Cancel;
        }        
    }
}